package pkgBFPaint;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;



public class MyCanvas extends Canvas implements MouseListener , MouseMotionListener{

	private Color currentDrawingColor = Color.black;
	private int currentTypeOfDrawing = ActionID.DRAWLINE;
	private boolean released=false , overview = false, polygonDrawn = false;
	
	private LinkedList<Action> actions = new LinkedList<Action>();
	
	MyCanvas(){
		setBackground( Color.WHITE );
		addMouseMotionListener(this);
		addMouseListener(this);
	}
	
	public void clear(){
		actions.clear();
		repaint();
	}
	
	public void removeLastAction(){
		Action temp;
		
		if( !actions.isEmpty() ){
			temp = actions.getLast();
			if( temp.isEmpty() || (!(temp.getActionID()==ActionID.DRAWELLIPSE) && !(temp.getActionID()==ActionID.DRAWRECTANGLE))){
				actions.removeLast();
			}else{
				temp.removeLastTwo();
			}
		}
	}
	
	public void setCurrentDrawingColor( Color c ){
		currentDrawingColor = c;
	}
	
	public Color getCurrentDrawingColor(){
		return currentDrawingColor;
	}
	
	public void setBackground(Color color) {
		for (int i = 0; i < actions.size(); i++) {
			if (actions.get(i).getActionID() == ActionID.DRAWRUBBER)
				((DrawRubber)(actions.get(i))).setColor(color);
		}
		super.setBackground(color);
	}
	
	public void setCurrentTypeOfDrawing( int newCurrent ){
		currentTypeOfDrawing = newCurrent;
	}
	
	public int getCurrentTypeOfDrawing(){
		return currentTypeOfDrawing;
	}
		
	public void paint(Graphics g){
		int i;
				
		for(i=0; i < actions.size() ; i++ ){
			actions.get(i).draw( g );
		}
	}
	
	public boolean actionNotAlreadyCreated( int actionType , Color c ){
		
		Action last;
		
		if( actions.isEmpty()) return true;
		
		last = actions.getLast();
		
		if(!(  (last.getActionID()==actionType )  && (last.getColor() == c )  )  ){
			return true;
		}else{
			return false;
		}
	}
	
	
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			if (currentTypeOfDrawing == ActionID.DRAWPOLYGON) {
				if (actionNotAlreadyCreated(currentTypeOfDrawing, currentDrawingColor)) {
					actions.add(new DrawPolygon(currentDrawingColor));
				}
				actions.getLast().addPoint(actions.getLast().getFirstPoint());
				polygonDrawn = true;
				repaint();
			}
		}
	}


	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void mousePressed(MouseEvent e) {
		if( currentTypeOfDrawing == ActionID.DRAWELLIPSE ){
			if( actionNotAlreadyCreated( currentTypeOfDrawing , currentDrawingColor )){
				actions.add( new DrawEllipse( currentDrawingColor ));
			}
			actions.getLast().addPoint( new Point( e.getX() , e.getY()));
		}
		if( currentTypeOfDrawing == ActionID.DRAWRECTANGLE ){
			if( actionNotAlreadyCreated( currentTypeOfDrawing , currentDrawingColor )){
				actions.add( new DrawRectangle( currentDrawingColor ));
			}
			actions.getLast().addPoint( new Point( e.getX() , e.getY()));
		}
		if( currentTypeOfDrawing == ActionID.DRAWPOLYGON ){
			if(actionNotAlreadyCreated(ActionID.DRAWPOLYGON, currentDrawingColor ) || polygonDrawn) {
				actions.add( new DrawPolygon( currentDrawingColor ) );
				polygonDrawn = false;
			}
			actions.getLast().addPoint( new Point( e.getX(), e.getY()));
			repaint();
		}
		
	}


	public void mouseReleased(MouseEvent e) {
		if(currentTypeOfDrawing == ActionID.DRAWLINE || currentTypeOfDrawing == ActionID.DRAWRUBBER)
			released = true;
		else if( currentTypeOfDrawing == ActionID.DRAWELLIPSE || currentTypeOfDrawing == ActionID.DRAWRECTANGLE ){
			if( overview ){
				actions.getLast().changeLast(new Point( e.getX() , e.getY()));
			}else{
				actions.getLast().addPoint( new Point( e.getX() , e.getY()));
			}
			overview = false;
			repaint();
		}
		
	}


	public void mouseDragged(MouseEvent e) {

		if(currentTypeOfDrawing == ActionID.DRAWLINE){
			
			if(actionNotAlreadyCreated( currentTypeOfDrawing , currentDrawingColor ) || released ){
				actions.add( new DrawLine( currentDrawingColor ) );
				released = false;
			}
			
			actions.getLast().addPoint( new Point( e.getX() , e.getY() ));
			
			repaint();
		}
		
		if( currentTypeOfDrawing == ActionID.DRAWELLIPSE || currentTypeOfDrawing == ActionID.DRAWRECTANGLE){
			if( overview == false ){
				actions.getLast().addPoint( new Point( e.getX() , e.getY() ));
				overview = true;
			}else{
				actions.getLast().changeLast( new Point( e.getX() , e.getY() ) );
			}
			
			repaint();			
		}
		
		if( currentTypeOfDrawing == ActionID.DRAWPOLYGON ){
			if( actionNotAlreadyCreated(ActionID.DRAWPOLYGON, currentDrawingColor ) ){
				actions.add( new DrawPolygon( currentDrawingColor ) );
			}
			actions.getLast().addPoint( new Point( e.getX(), e.getY()));
			repaint();
		}
		
		if (currentTypeOfDrawing == ActionID.DRAWRUBBER) {
			if( actionNotAlreadyCreated(ActionID.DRAWRUBBER, currentDrawingColor) || released) {
				actions.add(new DrawRubber(currentDrawingColor));
				released = false;
			}
			actions.getLast().addPoint( new Point( e.getX(), e.getY()));
			repaint();
		}
		
	}


	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}